home *** CD-ROM | disk | FTP | other *** search
/ Hardcore Visual Basic 5.0 (2nd Edition) / Hardcore Visual Basic 5.0 - Second Edition (1997)(Microsoft Press).iso / Code / Settings.cls < prev    next >
Text File  |  1997-06-14  |  6KB  |  188 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "GSettings"
  6. Attribute VB_GlobalNameSpace = True
  7. Attribute VB_Creatable = True
  8. Attribute VB_PredeclaredId = False
  9. Attribute VB_Exposed = True
  10. Option Explicit
  11.  
  12. Public Enum EErrorSettings
  13.     eeBaseSettings = 13600  ' Settings
  14. End Enum
  15.  
  16. Const sSep = ","
  17. Const sWinSection = "Window List"
  18. Const sCtlSection = "Control List"
  19.  
  20. Sub RestoreWindow(ByVal hWnd, sTitle As String)
  21.     Dim sValue As String, f As Integer
  22.  
  23.     sValue = GetSetting(App.ExeName, sWinSection, sTitle, sEmpty)
  24.     ' Quit if no entry
  25.     If sValue = sEmpty Then Exit Sub
  26.  
  27.     Dim wp As WINDOWPLACEMENT, iPos As Integer
  28.     wp.showCmd = Val(MParse.GetToken(sValue, sSep))
  29.     If IsWindowVisible(hWnd) = False Then
  30.         wp.showCmd = SW_HIDE
  31.     End If
  32.     ' wp.Flags = 0 (no flags required)
  33.     wp.ptMinPosition.x = Val(MParse.GetToken(sEmpty, sSep))
  34.     wp.ptMinPosition.y = Val(MParse.GetToken(sEmpty, sSep))
  35.     wp.ptMaxPosition.x = Val(MParse.GetToken(sEmpty, sSep))
  36.     wp.ptMaxPosition.y = Val(MParse.GetToken(sEmpty, sSep))
  37.     wp.rcNormalPosition.Left = Val(MParse.GetToken(sEmpty, sSep))
  38.     wp.rcNormalPosition.Top = Val(MParse.GetToken(sEmpty, sSep))
  39.     wp.rcNormalPosition.Right = Val(MParse.GetToken(sEmpty, sSep))
  40.     wp.rcNormalPosition.bottom = Val(MParse.GetToken(sEmpty, sSep))
  41.     ' Remember to set length
  42.     wp.length = Len(wp)
  43.     ' Send all your settings to the system
  44.     f = SetWindowPlacement(hWnd, wp)
  45.  
  46. End Sub
  47.  
  48. Sub SaveWindow(ByVal hWnd, sTitle As String)
  49.     
  50.     Dim sValue As String, f As Boolean
  51.     Dim wp As WINDOWPLACEMENT
  52.     ' First set type length for Windows
  53.     wp.length = Len(wp)
  54.     ' Get coordinates and other data about the window
  55.     f = GetWindowPlacement(hWnd, wp)
  56.     ' Read and use the data
  57.     sValue = wp.showCmd & ","
  58.     sValue = sValue & wp.ptMinPosition.x & ","
  59.     sValue = sValue & wp.ptMinPosition.y & ","
  60.     sValue = sValue & wp.ptMaxPosition.x & ","
  61.     sValue = sValue & wp.ptMaxPosition.y & ","
  62.     sValue = sValue & wp.rcNormalPosition.Left & ","
  63.     sValue = sValue & wp.rcNormalPosition.Top & ","
  64.     sValue = sValue & wp.rcNormalPosition.Right & ","
  65.     sValue = sValue & wp.rcNormalPosition.bottom
  66.     
  67.     SaveSetting App.ExeName, sWinSection, sTitle, sValue
  68.  
  69. End Sub
  70.  
  71. Sub ClearWindowSetting(sTitle As String)
  72.     DeleteSetting App.ExeName, sWinSection, sTitle
  73. End Sub
  74.  
  75. ' These could have been implemented with Form properties, but
  76. ' API does a better job of handling minimized and maximized windows.
  77.  
  78. ' Call only in Form_Load before Show
  79. #If fComponent Then
  80. Sub RestoreForm(frm As Object, Optional sTitle As String)
  81. #Else
  82. Sub RestoreForm(frm As Form, Optional sTitle As String)
  83. #End If
  84.     If sTitle = sEmpty Then sTitle = frm.Caption
  85.     RestoreWindow frm.hWnd, sTitle
  86. End Sub
  87.  
  88. ' Call only in Form_Unload when form is closing
  89. #If fComponent Then
  90. Sub SaveForm(frm As Object, Optional sTitle As String)
  91. #Else
  92. Sub SaveForm(frm As Form, Optional sTitle As String)
  93. #End If
  94.     If sTitle = sEmpty Then sTitle = frm.Caption
  95.     SaveWindow frm.hWnd, sTitle
  96. End Sub
  97.  
  98. #If fComponent Then
  99. Sub ClearFormSetting(frm As Object, Optional sTitle As String)
  100. #Else
  101. Sub ClearFormSetting(frm As Form, Optional sTitle As String)
  102. #End If
  103.     If sTitle = sEmpty Then sTitle = frm.Caption
  104.     DeleteSetting App.ExeName, sWinSection, sTitle
  105. End Sub
  106.  
  107. #If fComponent Then
  108. Sub RestoreCtl(ctl As Object, sTitle As String)
  109. #Else
  110. Sub RestoreCtl(ctl As Control, sTitle As String)
  111. #End If
  112. With ctl
  113.     Dim sValue As String, s As String, i As Long
  114.     sValue = GetSetting(App.ExeName, sCtlSection, sTitle, sEmpty)
  115.     ' Quit if no entry
  116.     If sValue = sEmpty Then Exit Sub
  117.  
  118.     ' Set left and top while in normal mode
  119.     On Error Resume Next
  120.     .Left = MParse.GetToken(sValue, sSep)
  121.     .Top = MParse.GetToken(sEmpty, sSep)
  122.     .Width = MParse.GetToken(sEmpty, sSep)
  123.     .Height = MParse.GetToken(sEmpty, sSep)
  124.     .Enabled = MParse.GetToken(sEmpty, sSep)
  125.     .Visible = MParse.GetToken(sEmpty, sSep)
  126.     s = .Caption
  127.     If Err = 0 Then .Caption = MParse.GetToken(sEmpty, sSep)
  128.     i = .BackColor
  129.     If Err = 0 Then .BackColor = MParse.GetToken(sEmpty, sSep)
  130.     i = .ForeColor
  131.     If Err = 0 Then .ForeColor = MParse.GetToken(sEmpty, sSep)
  132.     ' Enhance to restore anything else you saved
  133.  
  134. End With
  135. End Sub
  136.  
  137. #If fComponent Then
  138. Sub SaveCtl(ctl As Object, sTitle As String)
  139. #Else
  140. Sub SaveCtl(ctl As Control, sTitle As String)
  141. #End If
  142. With ctl
  143.     Dim sValue As String, s As String, i As Long
  144.  
  145.     On Error Resume Next
  146.     sValue = sValue & .Left & "," & .Top & ","
  147.     sValue = sValue & .Width & "," & .Height & ","
  148.     sValue = sValue & .Enabled & ","
  149.     sValue = sValue & .Visible & ","
  150.     s = .Caption
  151.     If Err = 0 Then sValue = sValue & .Caption & ","
  152.     i = .BackColor
  153.     If Err = 0 Then sValue = sValue & .BackColor & ","
  154.     i = .ForeColor
  155.     If Err = 0 Then sValue = sValue & .ForeColor & ","
  156.     
  157.     ' Enhance to save anything else you need
  158.  
  159.     SaveSetting App.ExeName, sCtlSection, sTitle, sValue
  160.  
  161. End With
  162. End Sub
  163.  
  164. ' Add more save and restore functions:
  165. '     SaveFont/RestoreFont
  166. '     SaveTextBox/RestoreTextBox
  167.  
  168. #If fComponent = 0 Then
  169. Private Sub ErrRaise(e As Long)
  170.     Dim sText As String, sSource As String
  171.     If e > 1000 Then
  172.         sSource = App.ExeName & ".Settings"
  173.         Select Case e
  174.         Case eeBaseSettings
  175.             BugAssert True
  176.        ' Case ee...
  177.        '     Add additional errors
  178.         End Select
  179.         Err.Raise COMError(e), sSource, sText
  180.     Else
  181.         ' Raise standard Visual Basic error
  182.         sSource = App.ExeName & ".VBError"
  183.         Err.Raise e, sSource
  184.     End If
  185. End Sub
  186. #End If
  187.  
  188.